package com.yurqi.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.*
import android.widget.EditText
import com.ug_project.views.*

/**
 * Created by yurqi on 3/12/2017.
 */

open class SDialog : Dialog {
    constructor(context: Context, resId: Int) : super(context) {
        this.resId = resId
        rootView = LayoutInflater.from(this.context).inflate(resId, null)
    }

    constructor(context: Context, resId: Int, style: Int) : super(context, style) {
        this.resId = resId
        rootView = LayoutInflater.from(this.context).inflate(resId, null)
    }

    internal var resId: Int = 0
    val match_parent = ViewGroup.LayoutParams.MATCH_PARENT
    val wrap_content = ViewGroup.LayoutParams.WRAP_CONTENT
    var width = ViewGroup.LayoutParams.WRAP_CONTENT
    var height = ViewGroup.LayoutParams.WRAP_CONTENT
    var rootView: View
    var isTouchCancel = false
    var backgroundDim = 0.5f

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(rootView)
        var window = this.window
        window.setGravity(Gravity.CENTER)
        var layoutParams = window.attributes

        layoutParams.width = width
        layoutParams.height = height
        window.setDimAmount(backgroundDim)
        window.attributes = layoutParams


        setCanceledOnTouchOutside(isTouchCancel)


    }


}

fun confirmDialog(context: Context,
                  title: String,
                  yesStr: String = "ھەئە",
                  noStr: String = "ياق",
                  yes: () -> Unit) {
    SDialog(context, R.layout.dialog_confrim).apply {
        isTouchCancel = false
        var _yes = rootView.findButton(R.id.yes)
        var _no = rootView.findButton(R.id.no)
        var _title = rootView.findTextView(R.id.title)

        _yes.text = yesStr
        _no.text = noStr
        _title.text = title
        _yes.setOnClickListener {
            yes()
            this.dismiss()
        }
        _no.setOnClickListener {
            this.dismiss()
        }
    }.show()

}

fun messageDialog(context: Context,
                  yesStr: String = "بىلدىم",
                  title: String, click: (() -> Unit)? = null) {
    SDialog(context, R.layout.dialog_complete).apply {
        var _yes = rootView.findButton(R.id.yes)
        var _title = rootView.findTextView(R.id.title)
        _title.text = title
        _yes.text = yesStr

        _yes.setOnClickListener {
            click?.invoke()
            this.dismiss()
        }

    }.show()
}



fun editDialog(context: Context,
               yesStr: String = "تامام",
               noStr: String = "بىكار قىلىش",
               editContent: String = "",
               title: String = "",
               action: SDialog.(content: String) -> Unit) {
    var test = ""
    SDialog(context, R.layout.dialog_edit).apply {
        width = ViewGroup.LayoutParams.MATCH_PARENT

        var _yes = rootView.findButton(R.id.yes)
        var _no = rootView.findButton(R.id.no)
        var _title = rootView.findTextView(R.id.title)
        var _editContent = rootView.findViewById(R.id.edit_content) as EditText

        _yes.text = yesStr
        _no.text = noStr
        _title.text = title
        _editContent.hint = editContent

        _yes.click = {
            action(_editContent.content)
            this.dismiss()
        }
        _no.click = {
            this.dismiss()
        }

    }.show()
}

fun editDialog(context: Context,
               yesStr: String = "تامام",
               noStr: String = "بىكار قىلىش",
               edit1: String = "",
               edit2: String = "",
               title: String = "",
               action: SDialog.(edit1: String, edit2: String) -> Unit) {

    SDialog(context, R.layout.dialog_edit_two).apply {
        width = ViewGroup.LayoutParams.MATCH_PARENT

        var _yes = rootView.findButton(R.id.yes)
        var _no = rootView.findButton(R.id.no)
        var _title = rootView.findTextView(R.id.title)
        var _edit1 = rootView.findViewById(R.id.edit1) as EditText
        var _edit2 = rootView.findViewById(R.id.edit2) as EditText

        _yes.text = yesStr
        _no.text = noStr
        _title.text = title
        _edit1.hint = edit1
        _edit2.hint = edit2

        _yes.click = {
            action(_edit1.content, _edit2.content)
            this.dismiss()
        }
        _no.click = {
            this.dismiss()
        }

    }.show()
}

